using System.Linq;
using NUnit.Framework;
using WWB.SitePointPrivateMessages.Core.Model.DataImport;
using WWB.SitePointPrivateMessages.Core.Model.Messages;

namespace WWB.SitePointPrivateMessages.Tests.DataImportTests
{
    public static class ImportTestingExtensions
    {
        public static void AssertPrivateMessageMatches(this ExportedMessage original, PrivateMessage compareTo)
        {
            verifyPrivateMessage(original, compareTo);
        }

        public static void AssertPrivateMessageMatches(this PrivateMessage created, ExportedMessage original)
        {
            verifyPrivateMessage(original, created);
        }

        public static void AssertPrivateMessageMatchesWithFolder(this ExportedMessage original, PrivateMessage created, string folder)
        {
            verifyPrivateMessage(original, created);
            Assert.That(created.Folder, Is.Not.Null, "Folder is null.");
            Assert.That(created.Folder.Name, Is.EqualTo(folder));
        }

        private static void verifyPrivateMessage(ExportedMessage original, PrivateMessage compareTo)
        {
            Assert.That(original.Title, Is.EqualTo(compareTo.Title));
            Assert.That(original.DateStamp, Is.EqualTo(compareTo.DateStamp.ToString("yyyy-MM-dd HH:mm")));
            Assert.That(original.FromUserId, Is.EqualTo(compareTo.FromUser.Id));
            Assert.That(original.FromUser, Is.EqualTo(compareTo.FromUser.Name));
            string[] toUsers = original.ToUser.Split(",".ToCharArray());
            Assert.That(toUsers.Length, Is.EqualTo(compareTo.ToUsers.Count()));
            foreach (var un in toUsers)
            {
                var usr = compareTo.ToUsers.FirstOrDefault(x => x.Name == un.Trim());
                Assert.That(usr != null, "Could not find matching user in compared PrivateMessage.");
            }
            Assert.That(original.MessageBody, Is.EqualTo(compareTo.Body));
        }
    }
}